package com.zretc.support.service.serviceimpl;

import com.zretc.support.dao.JoincourseDao;
import com.zretc.support.entity.Joincourse;
import com.zretc.support.service.JoincourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
public class JoincourseServiceImpl implements JoincourseService {

    @Autowired
    JoincourseDao joincourseDao;

    @Override
    public Page<Joincourse> getJoincourseByPageByStu(String keywords, int page ,int pagesize) {
        if (page < 1) {
            page = 1;
        }
        //-1是因为我们的PageRequest的参数是从0开始，0表示第一页
        Pageable pageable = PageRequest.of(page - 1, pagesize);

        return joincourseDao.getJoincourseByPageByStu(keywords,  pageable);
    }

    @Override
    public Page<Joincourse> getJoincourseByPageByCla(String keywords, int page ,int pagesize) {
        if (page < 1) {
            page = 1;
        }
        //-1是因为我们的PageRequest的参数是从0开始，0表示第一页
        Pageable pageable = PageRequest.of(page - 1, pagesize);

        return joincourseDao.getJoincourseByPageByCla(keywords,  pageable);
    }

    @Override
    public Page<Joincourse> getJoincourseByTeacherByStu(String userId,String keywords, int page, int pagesize) {
        if (page < 1) {
            page = 1;
        }
        //-1是因为我们的PageRequest的参数是从0开始，0表示第一页
        Pageable pageable = PageRequest.of(page - 1, pagesize);
        return joincourseDao.getJoincourseByTeacherByStu( userId, keywords, pageable);
    }

    @Override
    public Page<Joincourse> getJoincourseByTeacherByCla(String userId ,String keywords, int page, int pagesize) {
        if (page < 1) {
            page = 1;
        }
        //-1是因为我们的PageRequest的参数是从0开始，0表示第一页
        Pageable pageable = PageRequest.of(page - 1, pagesize);
        return joincourseDao.getJoincourseByTeacherByCla( userId,keywords, pageable);
    }

    @Override
    public Page<Joincourse> getJoincourseByStudent(String userId ,String keywords, int page, int pagesize) {
        if (page < 1) {
            page = 1;
        }
        //-1是因为我们的PageRequest的参数是从0开始，0表示第一页
        Pageable pageable = PageRequest.of(page - 1, pagesize);
        return joincourseDao.getJoincourseByStudent( userId,keywords, pageable);
    }

    @Override
    public Joincourse addJoincourse(Joincourse joincourse) {
        return joincourseDao.saveAndFlush(joincourse);
    }

    @Override
    public String deleteJoincourse(Long joincourseid) {

        if(joincourseDao.getOne(joincourseid)==null){
            return "没有该参课信息";
        }else{

            joincourseDao.deleteById(joincourseid);
            return "删除成功";
        }
    }
}
